#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Written by Mr. HuChang

import requests
from lxml import etree
from urllib.parse import quote
import json


if __name__ == '__main__':
    url = "https://gkcx.eol.cn/school/31"

    lua = '''function main(splash, args)
      splash.private_mode_enabled = false
      assert(splash:go("''' + url + '''"))
      assert(splash:wait(0.5))
      splash:set_user_agent("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36")
      return {
        html = splash:html(),
      }
    end'''

    response = requests.get("http://localhost:8888/execute?lua_source=" + quote(lua))
    html_text = json.loads(response.text)["html"]
    # print(len(html_text))

    html = etree.HTML(html_text)
    Map = {}

    Map['院校名称'] = \
    html.xpath('//*[@id="root"]/div/div/div/div/div/div/div[2]/div/div/div[3]/div[2]/div[1]/span')[
        0].text
    Map['院校所在地'] = \
    html.xpath('//*[@id="root"]/div/div/div/div/div/div/div[2]/div/div/div[3]/div[2]/div[2]/span//text()')[
        0]
    Map['人气值'] = html.xpath(
        '//*[@id="root"]/div/div/div/div/div/div/div[2]/div/div/div[3]/div[2]/div[2]/p/span[2]')[0].text
    school_atts = html.xpath(
        '//*[@id="root"]/div/div/div/div/div/div/div[2]/div/div/div[3]/div[2]/div[3]//div')
    atts = []
    for att in school_atts:
        atts.append(att.text)
    Map['学校属性'] = atts
    hrefs = html.xpath(
        '//*[@id="root"]/div/div/div/div/div/div/div[2]/div/div/div[3]/div[2]/div[4]/div[1]/span[2]//a')
    webs = []
    for href in hrefs:
        webs.append(href.text)
    Map['官方网址'] = webs
    phones = html.xpath(
        '//*[@id="root"]/div/div/div/div/div/div/div[2]/div/div/div[3]/div[2]/div[4]/div[2]/span[2]')[0].text
    Map['官方电话'] = phones.split('：')[1].split(',')

    email = html.xpath(
        '//*[@id="root"]/div/div/div/div/div/div/div[2]/div/div/div[3]/div[2]/div[4]/div[3]/span[2]')[0].text
    Map['电子邮箱'] = email.split('：')[1].split(',')
    Map['学习指数'] = html.xpath(
        '//*[@id="root"]/div/div/div/div/div/div/div[3]/div/div[1]/div/div/div[1]/div[2]/div[1]/div[1]/div/div/span')[
        0].text
    Map['生活指数'] = html.xpath(
        '//*[@id="root"]/div/div/div/div/div/div/div[3]/div/div[1]/div/div/div[1]/div[2]/div[1]/div[2]/div/div/span')[
        0].text
    Map['就业指数'] = html.xpath(
        '//*[@id="root"]/div/div/div/div/div/div/div[3]/div/div[1]/div/div/div[1]/div[2]/div[1]/div[3]/div/div/span')[
        0].text
    Map['综合评分'] = html.xpath(
        '//*[@id="root"]/div/div/div/div/div/div/div[3]/div/div[1]/div/div/div[1]/div[2]/div[1]/div[4]/div/div/span')[
        0].text

    dic = {}
    doctor_one = html.xpath('//*[@id="root"]/div/div/div/div/div/div/div[3]/div/div[1]/div/div/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/p[1]/span[1]')[0].text
    doctor_two = html.xpath('//*[@id="root"]/div/div/div/div/div/div[1]/div[3]/div/div[1]/div/div/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[2]/p[1]/span[1]')[0].text
    dic['博士点'] = {'一级': doctor_one, "二级": doctor_two}

    master_one = html.xpath('//*[@id="root"]/div/div/div/div/div/div[1]/div[3]/div/div[1]/div/div/div[2]/div[1]/div[1]/div[2]/div[1]/div[2]/div[1]/div[1]/p[1]/span[1]')[0].text
    master_two = html.xpath('//*[@id="root"]/div/div/div/div/div/div[1]/div[3]/div/div[1]/div/div/div[2]/div[1]/div[1]/div[2]/div[1]/div[2]/div[1]/div[2]/p[1]/span[1]')[0].text
    dic['硕士点'] = {'一级': master_one, "二级": master_two}

    dic['国家重点学科'] = html.xpath('//*[@id="root"]/div/div/div/div/div/div[1]/div[3]/div/div[1]/div/div/div[2]/div[1]/div[1]/div[2]/div[1]/div[3]/h3')[0].text
    dic['重点实验室'] = html.xpath('//*[@id="root"]/div/div/div/div/div/div[1]/div[3]/div/div[1]/div/div/div[2]/div[1]/div[1]/div[2]/div[1]/div[4]/h3')[0].text
    Map['基本信息'] = dic

    built = html.xpath('//*[@id="root"]/div/div/div/div/div/div[1]/div[3]/div/div[1]/div/div/div[2]/div[1]/div[1]/div[2]/div[2]/ul/li[1]/span[1]')[0].text.split('：')
    Map[built[0]] = built[1]

    area = html.xpath('//*[@id="root"]/div/div/div/div/div/div[1]/div[3]/div/div[1]/div/div/div[2]/div[1]/div[1]/div[2]/div[2]/ul/li[1]/span[2]')[0].text.split("：")
    Map[area[0]] = area[1]

    address = html.xpath('//*[@id="root"]/div/div/div/div/div/div[1]/div[3]/div/div[1]/div/div/div[2]/div[1]/div[1]/div[2]/div[2]/ul/li[3]/span')[0].text.split("：")
    Map[address[0]] = address[1]

    print(Map)
